package com.example.hanfu_bk.service;

import com.example.hanfu_bk.mapper.CategoryMapper;
import lombok.RequiredArgsConstructor;
import com.example.hanfu_bk.mapper.CategoryMapper;
import com.example.hanfu_bk.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    public List<Category> getAllCategories() {
        List<Category> categories = categoryMapper.findAllCategoriesWithDetails();

        // 处理空值情况
        return categories.stream()
                .filter(c -> c.getSubCategories() != null)
                .peek(c -> c.getSubCategories().removeIf(Objects::isNull))
                .collect(Collectors.toList());
    }
}
